﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IrrKlang;
using System.IO;

namespace temp_gui_2
{
    public partial class Form1 : Form, ISoundStopEventReceiver
    {
        ISoundEngine gediaEngine;
        ISoundSource gediaSource;
        ISound gediaPlaying;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            gediaEngine = new ISoundEngine(SoundOutputDriver.AutoDetect, SoundEngineOptionFlag.PrintDebugInfoIntoDebugger);
        }

        private void importFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            DialogResult dr = openFileDialog1.ShowDialog();
            if (!File.Exists(openFileDialog1.FileName))
            {
                MessageBox.Show("There was a problem opening the file","Apple is dum",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                return;
            }
            //MessageBox.Show(openFileDialog1.FileName);
            if (gediaEngine != null) gediaEngine.StopAllSounds();
            if (gediaPlaying != null) gediaPlaying.Stop();
            gediaEngine = null;
            gediaPlaying = null;
            gediaEngine = new ISoundEngine();
            gediaPlaying = gediaEngine.Play2D(openFileDialog1.FileName);
            //this.progressBar1.Maximum = (int)gediaPlaying.PlayLength;
            this.trackBar2.Maximum = 100;
            this.trackBar1.Maximum = (int)gediaPlaying.PlayLength;
            timer1.Start();
            gediaPlaying.setSoundStopEventReceiver(this);
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (gediaEngine == null || gediaPlaying == null) return;
            else
            {
                float value = (float)this.trackBar2.Value / 100;
                gediaPlaying.Volume = value;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.trackBar1.Value = (int)gediaPlaying.PlayPosition;
            
        }
        public void OnSoundStopped(ISound sound,StopEventCause cause,object sender)
        {
            timer1.Stop();
            gediaPlaying = null;
            gediaEngine = null;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (gediaEngine == null || gediaPlaying == null) return;
            else gediaPlaying.PlayPosition = (uint)this.trackBar1.Value;
        }
    }
    
}
